package org.jeecg.modules.user.home.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GlobalCoordinates;
import org.jeecg.modules.user.home.dto.AppHomePageRes;
import org.jeecg.modules.user.home.dto.StationNameReq;
import org.jeecg.modules.user.home.mapper.AppHomeMapper;
import org.jeecg.modules.user.home.service.AppHomeService;
import org.jeecg.modules.param.pile.service.IBikePileService;
import org.jeecg.modules.param.station.service.IBikeStationService;
import org.jeecg.modules.utils.DistanceUtils;
import org.jeecg.modules.utils.BikeResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

@Service
public class AppHomeServiceImpl implements AppHomeService {
    @Autowired
    private IBikeStationService bikeStationService;

    @Autowired
    private IBikePileService bikePileService;

    @Resource
    private AppHomeMapper appHomeMapper;




    @Override
    public BikeResult getAppHomePageList(StationNameReq stationNameReq) {
        double longitude1 = stationNameReq.getLongitude1();
        double latitude1 = stationNameReq.getLatitude1();
        //System.out.println(longitude1+","+latitude1);
        long heartTime = new Date().getTime() - 60000;
        stationNameReq.setHeartTime(new Date(heartTime));
        List<AppHomePageRes> list = appHomeMapper.getAppHomeList(stationNameReq);
        if (list.size() > 0) {
            //double[] doubles = DistanceUtils.GCJ02ToWGS84(stationNameReq.getLongitude1(), stationNameReq.getLatitude1());
            //System.out.println(doubles[0]+","+doubles[1]);
            for (AppHomePageRes blist : list) {
                if (latitude1 != 0.0 && longitude1 != 0.0) {
                    //增加距离
                    if (StringUtils.isNotEmpty(blist.getLongitude()) && StringUtils.isNotEmpty(blist.getLatitude())) {
                        //double distance = DistanceUtils.getDistanceMeter(
                        //        new GlobalCoordinates(doubles[0], doubles[1]),
                        //        new GlobalCoordinates(Double.parseDouble(blist.getLongitude()), Double.parseDouble(blist.getLatitude())), Ellipsoid.WGS84);
                        //double distance = DistanceUtils.getDistance(doubles[0], doubles[1], Double.parseDouble(blist.getLongitude()), Double.parseDouble(blist.getLatitude()));

                        double distance = DistanceUtils.getDistance(longitude1, latitude1, Double.parseDouble(blist.getLongitude()), Double.parseDouble(blist.getLatitude()));

                        String format = new DecimalFormat("0.0").format(distance);
                        blist.setDistance(format);
                        blist.setSort(BigDecimal.valueOf(Double.parseDouble(format)));
                    }
                }
            }
            list.sort(new Comparator<AppHomePageRes>() {
                @Override
                public int compare(AppHomePageRes appHomePageRes, AppHomePageRes t1) {
                        return appHomePageRes.getSort().compareTo(t1.getSort());
                }
            });
            for (int i = 1 ; i <= list.size();i++){
                AppHomePageRes appHomePageRes = list.get(i - 1);
                appHomePageRes.setNumber(i+"");
            }
            return BikeResult.ok("1", "查询成功", list);
        }
        return BikeResult.ok("1", "查询成功", null);
    }

}
